//获取应用实例
const app = getApp()
const api = require('../../../api/index.js')
const util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    tabData:[
      {
        name: "个人提升",
        active: true,
        checked: 'true',
        phone:'',
        button:false
      },
      {
        name: "亲子教学",
        active: false,
        phone: '',
        button: false
      },
      {
        name: "团队聚会",
        active: false,
        phone: '',
        button: false
      }
    ],
    indexFormOne: {},
    indexFormTwo: {},
    indexFormThree: {},
    indexData: {
      active: [],
      branch: [],
      classify: [],
      course: []
    }, // 首页请求数据
    btnActive: [false, false, false],
    address: { region_name: '北京市'}, //当前位置
    imgSrc: "",
    hideView:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // app.message();
    let invite_id = options.invite_id;
    console.log(options,'测试分享数据')
    if (invite_id){
      wx.setStorageSync('invite_id', invite_id)
    }
    console.log(invite_id);
    this.setData({
      imgSrc: api.imgPathUrl
    })
    app.locationCity()
  },
  onItemClick: function (e) {
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.dataset.postid;
    console.log(id)
    let urll = e.currentTarget.dataset.url;

    wx.navigateTo({
      url: "/pages/activity/activity/activity?url=" + urll
    })
    // console.log(e.currentTarget.dataset,id,name)

    App.zhuge.track('记录首页baner-点击调取活动名称', {
      '活动名称': name,
      '活动ID': id
    });

  },
  phoneSYz: function (obj,index) {
    console.log(obj)
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(obj)) {
        this.setData({
          [`btnActive[${index}]`]: false
        })
        console.log(index);
        return false;
    } else {
        this.setData({
          [`btnActive[${index}]`]: true
        })
        return true;
    }
  },
  phones:function(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      [`tabData[${index}].phone`]: e.detail.value
    })
    let phon = e.detail.value;
    this.phoneSYz(phon,index);

  },
  toPlain:function(e){
      console.log(e)
      let index = e.target.dataset.index;
      let phone = this.data.tabData[index].phone;
      let typeState = e.target.dataset.type;
      let num = Number(index)+1;
      let data = {
        type: num,
        mobile: phone
      }
      if (typeState){
        this.indexForm(data)
      }    
  },
  indexForm: function (data){
    api.indexForm(data,(res)=>{
      console.log(res);
      if(res.data.code == 200){
        this.setData({
          hideView: true
        })
      }
    })
  },
  indexOk:function(){
    this.setData({
      hideView:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
 
  onHide: function () {  // 生命周期函数 - 页面卸载
    App.zhuge.track('首页面进入后台', {
      '首页进入后台': '首页进入后台'
    });
  },  
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {

  },
  radioChange: function (e) {

    let index = e.currentTarget.dataset.index;
    this.data.tabData.forEach((item,index) =>{
      let key = `tabData[${index}].active`;
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`tabData[${index}].active`]: true
    })
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  seacClick:function(){
    App.zhuge.track('记录点击.首页搜索栏', {});
  },
  onItemClick:function(e){
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.dataset.postid;
    console.log(id)
    let urll = e.currentTarget.dataset.url;

    wx.navigateTo({
      url: "/pages/activity/activity/activity?url=" + urll
    })
    // console.log(e.currentTarget.dataset,id,name)

    App.zhuge.track('记录首页baner-点击调取活动名称', {
      '活动名称':name,
      '活动ID':id
    });
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.onLoad();
    this.getData(); // 获取首页数据
    App.zhuge.track('记录到达.首页', {
      '记录到达.首页': '记录到达.首页'
    });
    //console.log(app.globalData);
  },
  getData: function() { // 获取首页数据
    if (app.globalData.currentCity){
      let data = {
        d_id: app.globalData.currentCity.region_id
      }
      api.index(1, (res) => {
        console.log(res)
        this.setData({
          "indexData": res.data.result
        })
      })
      this.setData({
        address: app.globalData.currentCity
      })
    }else {
      api.index({}, (res) => {
        this.setData({
          "indexData": res.data.result
        })
      })
    } 
  },
  phoneCall: function(e){ // 底部打电话
    let phone;
    let data = e.currentTarget.dataset.phone;
    App.zhuge.track(' 合作咨询电话',{});
    util.phone(data, this.data.indexData.cooperation.mobile);
    
  },
  toAddress: function(){ // 定位地址跳转
    let city = this.data.indexData.region;
    // wx.navigateTo({
    //   url: '/pages/index/cityList/cityList',
    // })
  },
  toVennue: function(e){ // 点击项目分类跳往找场馆 
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    App.zhuge.track('首页分类', {
      '首页分类': name
    })
    app.globalData.vennueQuery = {
      id: id,
      name: name
    }
    console.log(id + name)
    wx.switchTab({
      url: `/pages/lookVennue/lookVennue/lookVennue`,
    })
  },
  getPhoneNumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  } 
})